package kd.cosmic.formplugin;

import com.kingdee.util.StrUtil;
import com.kingdee.util.StringUtils;
import kd.bos.context.RequestContext;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.form.control.Button;
import kd.bos.form.control.Control;
import kd.bos.form.control.events.BeforeClickEvent;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.DeleteServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;
import kd.sdk.plugin.Plugin;

import java.util.EventObject;

/**
 * 动态表单
 */
public class schoolCardShowPage extends AbstractFormPlugin{

    @Override
    public void afterCreateNewData(EventObject e) {
        super.afterCreateNewData(e);

        String userId = String.valueOf(RequestContext.get().getCurrUserId());
        String userName = String.valueOf(RequestContext.get().getUserName());
        DynamicObject tempInfo = BusinessDataServiceHelper.loadSingle("xhzh_school_card",
                "xhzh_idnumber,xhzh_cardid,status"
                , new QFilter[]{new QFilter("number", QCP.equals, userId)});
        this.getModel().setValue("xhzh_name",userName);
        this.getModel().setValue("xhzh_id",userId);

        this.getView().setEnable(false,"xhzh_name");
        this.getView().setEnable(false,"xhzh_id");
        this.getView().setEnable(false,"xhzh_idnumber");
        this.getView().setEnable(false,"xhzh_cardnumber");
        this.getView().setEnable(false,"xhzh_status");
        if(tempInfo == null){
            return;
        }
        this.getModel().setValue("xhzh_idnumber",tempInfo.getString("xhzh_idnumber"));
        this.getModel().setValue("xhzh_cardnumber",tempInfo.getString("xhzh_cardid"));
        this.getModel().setValue("xhzh_status",tempInfo.getString("status"));


    }

    @Override
    public void registerListener(EventObject e) {
        super.registerListener(e);
        Button xhzhLost = this.getView().getControl("xhzh_lost");
        Button xhzhDelete = this.getView().getControl("xhzh_delete");
        Button xhzhClost = this.getView().getControl("xhzh_cancel_lose");
        xhzhLost.addClickListener(this);
        xhzhDelete.addClickListener(this);
        xhzhClost.addClickListener(this);
    }

    @Override
    public void beforeClick(BeforeClickEvent evt) {
        super.beforeClick(evt);
        Control source = (Control) evt.getSource();
        String userId = String.valueOf(RequestContext.get().getCurrUserId());
        DynamicObject tempInfo = BusinessDataServiceHelper.loadSingle("xhzh_school_card",
                "status"
                , new QFilter[]{new QFilter("number", QCP.equals, userId)});
        if(StringUtils.equals("xhzh_lost",source.getKey())){
            if (tempInfo != null && tempInfo.get("status").equals("挂失")) {
                this.getView().showMessage("该卡已处于挂失状态");
                evt.setCancel(true);
            }else if (tempInfo == null){
                this.getView().showMessage("您还未申请校园卡，请申请");
                evt.setCancel(true);
            }else {
                return;
            }
        }
        if (StringUtils.equals("xhzh_cancel_lose",source.getKey())) {
            if (tempInfo != null && tempInfo.get("status").equals("已审核")) {
                this.getView().showMessage("该卡已处于激活状态");
                evt.setCancel(true);
            }else if (tempInfo == null){
                this.getView().showMessage("您还未申请校园卡，请申请");
                evt.setCancel(true);
            }else {
                return;
            }
        }
        if (StringUtils.equals("xhzh_delete",source.getKey())) {
            if (tempInfo == null){
                this.getView().showMessage("您还未申请校园卡，请申请");
                evt.setCancel(true);
            }else {
                return;
            }
        }
    }

    @Override
    public void click(EventObject evt) {
        super.click(evt);
        Control source = (Control) evt.getSource();
        String userId = String.valueOf(RequestContext.get().getCurrUserId());
        //TODO 后期考虑增加流程服务插件
        if(StringUtils.equals("xhzh_lost",source.getKey())){
            DynamicObject[] tempInfo = BusinessDataServiceHelper.load("xhzh_school_card",
                    "status"
                    , new QFilter[]{new QFilter("number", QCP.equals, userId)});
            tempInfo[0].set("status","挂失");
            Object[] save = SaveServiceHelper.save(tempInfo);
            this.getView().invokeOperation("refresh");

        } else if (StringUtils.equals("xhzh_delete",source.getKey())) {

            int delete = DeleteServiceHelper.delete("xhzh_school_card",
                    new QFilter[]{new QFilter("number", QCP.equals, userId)});
            this.getView().showMessage(String.valueOf(delete));
            this.getView().showMessage("成功注销");
        } else if (StringUtils.equals("xhzh_cancel_lose",source.getKey())) {
            DynamicObject[] tempInfo = BusinessDataServiceHelper.load("xhzh_school_card",
                    "status"
                    , new QFilter[]{new QFilter("number", QCP.equals, userId)});
            tempInfo[0].set("status","已审核");
            Object[] save = SaveServiceHelper.save(tempInfo);
            this.getView().invokeOperation("refresh");
        }

    }
}